from PyQt4.QtGui import QMainWindow
from PyQt4.QtGui import QStyle, QActionGroup, QStyleFactory, QStyleFactory, QAction, qApp
from mainwindow_gen import Ui_MainWindow
from PyQt4.QtCore import QEvent, QRect
from PyQt4.QtGui import QStyle, QStyleOptionSlider

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        # setup generated file
        self.setupUi(self)

        ### gui tuning
        # buttons
        self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
        self.nextButton.setIcon(self.style().standardIcon(QStyle.SP_MediaSkipForward))
        self.prevButton.setIcon(self.style().standardIcon(QStyle.SP_MediaSkipBackward))
        self.shuffleButton.setIcon(self.style().standardIcon(QStyle.SP_FileDialogListView))
        self.repeatButton.setIcon(self.style().standardIcon(QStyle.SP_BrowserReload))
        self.clearButton.setIcon(self.style().standardIcon(QStyle.SP_DialogResetButton))

        # handle events of sliders
        self.seekBar.installEventFilter(self)
        self.volumeBar.installEventFilter(self)

        # catch styles
        group = QActionGroup(self)
        for style in QStyleFactory.keys():
            action = QAction(style, self)
            action.setCheckable(True)

            # select current style
            if style.toLower() == qApp.style().objectName():
                action.setChecked(True)

            action.setProperty("style", style)
            group.addAction(action)
            self.menuThemes.addAction(action)

        group.triggered.connect(self.setStyle)

        # connect roomsbox
        self.roomsBox.currentIndexChanged.connect(self.stackedWidget.setCurrentIndex)


    def setStyle(self, action):
        qApp.setStyle(action.property("style").toString())


    def eventFilter(self, object, event):
    # handle mouse events and make the instant movement of the slider  handle
        if event.type() == QEvent.MouseButtonPress or event.type() == QEvent.MouseMove:
            if object == self.seekBar or object == self.volumeBar:
                s = object
                option = QStyleOptionSlider()
                seekBarRect = self.style().subControlRect(QStyle.CC_ScrollBar, option, QStyle.SC_SliderHandle, self.seekBar)
                s.setValue(QStyle.sliderValueFromPosition(s.minimum(), s.maximum(),event.pos().x() - seekBarRect.width() / 2 ,
                                                          s.width() - seekBarRect.width() -1))
                event.accept()
                return True

        return False

